package com.huishu.commons.utlis;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;

/**
 * @ClassName：ExportUtil
 * @Author: ljj
 * @Date: 2022/6/9 9:16
 */
public class ExportUtil {

    public static void export(HttpServletResponse response, Class clazz, Collection<?> data){
        try {
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("execlInfo", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 集合线程同步用--线程安全List
            EasyExcel.write(response.getOutputStream(), clazz).sheet("productExecl").doWrite(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
